const person = document.querySelector('.person')
const logoutbox = document.querySelector('.logoutbox')
const person_show = document.querySelector(".person_show")
const headright = document.querySelector('.headright')
const buyer_show = document.querySelector('.buyer_show')
const seller_show = document.querySelector('.seller_show')
const buyer = document.querySelector('.buyer')
const seller = document.querySelector('.seller')
const head_car = document.querySelector('.head_car')
const person_name = document.querySelector('.nologin>.person_name')
const id = window.localStorage.getItem('id')
person.onmouseover = function(){
    logoutbox.style.display = 'block'
    person_show.classList.add('hover')
}
person.onmouseout= function(){
    logoutbox.style.display = 'none'
    person_show.classList.remove('hover')
}
buyer.onmouseover = function(){
    buyer_show.style.display = 'block'
    seller_show.style.display = 'none'
}
buyer.onmouseout = function(){
    buyer_show.style.display = 'none'
    seller_show.style.display = 'none'
}
seller.onmouseover = function(){
    buyer_show.style.display = 'none'
    seller_show.style.display = 'block'
}
seller.onmouseout = function(){
    buyer_show.style.display = 'none'
    seller_show.style.display = 'none'
}
head_car.onclick = function(){
    location.href = './car.html'
}
person_name.onclick = function(){
    location.href = './login.html'
}
$('.headright>.index').click(function(){
    location.href = './index.html'
})
$('.logout>a').click(function(){
    $.ajax({
        url:'http://localhost:8888/users/logout',
        data:{id:id},
        success:function(res){
            if(res.code === 1){
                alert(res.message)
                $('.logined').css('display','none')
                $('.nologin').css('display','block')
            }
        }
    })
})
const token = window.localStorage.getItem("token")
$.ajax({
    url:'http://localhost:8888/users/info',
    data:{id:id},
    headers:{authorization:token},
    success:function(res){
        console.log(res)
        if(res.code == 1){
            console.log($('.logined>.person_show>.person_name').text())
            $('.logined').css('display','block')
            $('.logined>.person_show>.person_name').text(res.info.nickname)
            $('.nologin').css('display','none')
        }else{
            alert('登录过期了，请重新登录！')
            $('.logined').css('display','none')
            $('.nologin').css('display','block')
        }
    },
    error:function(err){
        console.log(err)
    }
})
